
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
        <meta name="description" content="Starry设计文档">
      
      
      
      
        <link rel="prev" href="../%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/Starry%E7%BB%93%E6%9E%84%E8%AF%B4%E6%98%8E/">
      
      
        <link rel="next" href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E5%86%85%E6%A0%B8%E5%BA%95%E5%B1%82%E6%A8%A1%E5%9D%97-axhal/">
      
      <link rel="icon" href="../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.5.2, mkdocs-material-9.1.21">
    
    
      
        <title>设计思路 - Starry</title>
      
    
    
      <link rel="stylesheet" href="../assets/stylesheets/main.eebd395e.min.css">
      
        
        <link rel="stylesheet" href="../assets/stylesheets/palette.ecc896b0.min.css">
      
      

    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
      <link rel="stylesheet" href="../resources/css/extra.css">
    
    <script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="blue" data-md-color-accent="cyan">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

  

<header class="md-header md-header--shadow" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href=".." title="Starry" class="md-header__button md-logo" aria-label="Starry" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            Starry
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              设计思路
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="blue" data-md-color-accent="cyan"  aria-label="切换至夜间模式"  type="radio" name="__palette" id="__palette_1">
            
              <label class="md-header__button md-icon" title="切换至夜间模式" for="__palette_2" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
              </label>
            
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="black" data-md-color-accent="cyan"  aria-label="切换至日间模式"  type="radio" name="__palette" id="__palette_2">
            
              <label class="md-header__button md-icon" title="切换至日间模式" for="__palette_1" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
              </label>
            
          
        </form>
      
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
        <button type="reset" class="md-search__icon md-icon" title="Clear" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            Initializing search
          </div>
          <ol class="md-search-result__list" role="presentation"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href=".." title="Starry" class="md-nav__button md-logo" aria-label="Starry" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    Starry
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href=".." class="md-nav__link">
        Starry
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A6%82%E8%BF%B0/" class="md-nav__link">
        概述
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_3" >
      
      
      
        <label class="md-nav__link" for="__nav_3" id="__nav_3_label" tabindex="0">
          架构设计
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_3">
          <span class="md-nav__icon md-icon"></span>
          架构设计
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/ArceOS%E4%BB%8B%E7%BB%8D/" class="md-nav__link">
        ArceOS介绍
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/Starry%E7%BB%93%E6%9E%84%E8%AF%B4%E6%98%8E/" class="md-nav__link">
        Starry结构说明
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          设计思路
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        设计思路
      </a>
      
        

<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
  
  
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      Table of contents
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    模块划分
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    进程引入
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    地址空间引入
  </a>
  
    <nav class="md-nav" aria-label="地址空间引入">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    任务切换
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    特权级切换
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    特权级切换
  </a>
  
    <nav class="md-nav" aria-label="特权级切换">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    初始化进入
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#trap" class="md-nav__link">
    trap切换
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_5" >
      
      
      
        <label class="md-nav__link" for="__nav_5" id="__nav_5_label" tabindex="0">
          模块介绍
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_5">
          <span class="md-nav__icon md-icon"></span>
          模块介绍
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E5%86%85%E6%A0%B8%E5%BA%95%E5%B1%82%E6%A8%A1%E5%9D%97-axhal/" class="md-nav__link">
        内核底层模块-axhal
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%A8%A1%E5%9D%97-axmem/" class="md-nav__link">
        内存管理模块-axmem
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E7%BD%91%E7%BB%9C%E6%A8%A1%E5%9D%97-axnet/" class="md-nav__link">
        网络模块-axnet
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E4%BF%A1%E5%8F%B7%E6%A8%A1%E5%9D%97-axsignal/" class="md-nav__link">
        信号模块-axsignal
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E6%A8%A1%E5%9D%97-axtask/" class="md-nav__link">
        任务调度模块-axtask
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E6%A8%A1%E5%9D%97-axfs/" class="md-nav__link">
        文件系统模块-axfs
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E8%BF%9B%E7%A8%8B%E6%8E%A7%E5%88%B6%E6%A8%A1%E5%9D%97-axprocess/" class="md-nav__link">
        进程控制模块-axprocess
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E5%86%85%E6%A0%B8%E8%BF%90%E8%A1%8C%E6%97%B6%E6%A8%A1%E5%9D%97-axruntime/" class="md-nav__link">
        内核运行时模块-axruntime
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/%E5%AE%8F%E5%86%85%E6%A0%B8%E7%94%A8%E6%88%B7%E5%BA%93-starry/" class="md-nav__link">
        宏内核用户库-starry
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_6" >
      
      
      
        <label class="md-nav__link" for="__nav_6" id="__nav_6_label" tabindex="0">
          实现重点
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_6">
          <span class="md-nav__icon md-icon"></span>
          实现重点
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%AE%9E%E7%8E%B0%E9%87%8D%E7%82%B9/%E4%BE%9D%E8%B5%96%E9%97%AE%E9%A2%98/" class="md-nav__link">
        依赖问题
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%AE%9E%E7%8E%B0%E9%87%8D%E7%82%B9/%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98/" class="md-nav__link">
        兼容问题
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%AE%9E%E7%8E%B0%E9%87%8D%E7%82%B9/%E5%AE%8F%E5%86%85%E6%A0%B8%E5%8C%96%E6%8E%A2%E8%AE%A8/" class="md-nav__link">
        宏内核化探讨
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../%E9%97%AE%E9%A2%98%E4%B8%8E%E8%A7%A3%E5%86%B3/" class="md-nav__link">
        问题与解决
      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
  
  
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      Table of contents
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    模块划分
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    进程引入
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    地址空间引入
  </a>
  
    <nav class="md-nav" aria-label="地址空间引入">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    任务切换
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    特权级切换
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    特权级切换
  </a>
  
    <nav class="md-nav" aria-label="特权级切换">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    初始化进入
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#trap" class="md-nav__link">
    trap切换
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


  <h1>设计思路</h1>

<h3 id="_1">模块划分<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>依据模块化内核的设计思想，我们期望我们的内核能够在尽可能保留原有unikernel的基础上同时兼容宏内核功能，因此我们在arceos的基础上新增了如下模块：</p>
<ul>
<li>axmem：引入地址空间</li>
<li>axtask/monolithic_task：修改原有task模块，添加了更多任务状态，从而使得任务可以作为线程的形式被进程调度</li>
<li>axsignal：信号模块</li>
<li>axprocess：引入进程概念，支持多进程多线程运行，添加了很多与Linux系统调用相关的内容</li>
<li>starry_libax：Linux系统调用用户库，包装了作为Linux兼容层的众多对外接口。</li>
</ul>
<h3 id="_2">进程引入<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<p>为了运行Linux相关的应用，我们需要让不同任务之间存在父子等关系，因此我们引入了进程的概念。在标准的Linux中，进程和线程统一用pthread结构体代替，但我们为了保证原有arceos的任务调度结构不受过大影响，因此选择将进程和线程进行分离，进程保存在独立的结构体<code>Process</code>中。</p>
<p>依据模块化的思想，我们可以将进程视为一个容器，存储了<strong>各类运行时资源，包括虚存、文件描述符、
线程、信号等</strong>。
在该种设计理念下，进程仅是对上述资源的一个统一与包装。因此可以通过添加 feature 等方式将进程作为一个可插拔模块，使得内核在宏内核架构与微内核架构中随时进行切换。</p>
<p>进程结构设计如下：</p>
<div class="highlight"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">ProcessInner</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="sd">/// 父进程的进程号</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">parent</span>: <span class="kt">u64</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 子进程</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">children</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">Arc</span><span class="o">&lt;</span><span class="n">Process</span><span class="o">&gt;&gt;</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 子任务</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">tasks</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">AxTaskRef</span><span class="o">&gt;</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 地址空间，由于存在地址空间共享，因此设计为Arc类型</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">memory_set</span>: <span class="nc">Arc</span><span class="o">&lt;</span><span class="n">SpinNoIrq</span><span class="o">&lt;</span><span class="n">MemorySet</span><span class="o">&gt;&gt;</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 用户堆基址，任何时候堆顶都不能比这个值小，理论上讲是一个常量</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">heap_bottom</span>: <span class="kt">usize</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 当前用户堆的堆顶，不能小于基址，不能大于基址加堆的最大大小</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">heap_top</span>: <span class="kt">usize</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 进程状态</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">is_zombie</span>: <span class="kt">bool</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 退出状态码</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">exit_code</span>: <span class="kt">i32</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 文件管理器，存储如文件描述符等内容</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;fs&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">fd_manager</span>: <span class="nc">FdManager</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 进程工作目录</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">cwd</span>: <span class="nb">String</span><span class="p">,</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;signal&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="sd">/// 信号处理模块    </span>
<span class="w">    </span><span class="sd">/// 第一维代表线程号，第二维代表线程对应的信号处理模块</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">signal_module</span>: <span class="nc">BTreeMap</span><span class="o">&lt;</span><span class="kt">u64</span><span class="p">,</span><span class="w"> </span><span class="n">SignalModule</span><span class="o">&gt;</span><span class="p">,</span>

<span class="w">    </span><span class="sd">/// robust list存储模块</span>
<span class="w">    </span><span class="sd">/// 用来存储线程对共享变量的使用地址</span>
<span class="w">    </span><span class="sd">/// 具体使用交给了用户空间</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">robust_list</span>: <span class="nc">BTreeMap</span><span class="o">&lt;</span><span class="kt">u64</span><span class="p">,</span><span class="w"> </span><span class="n">FutexRobustList</span><span class="o">&gt;</span><span class="p">,</span>
<span class="p">}</span>
</code></pre></div>
<p>任务结构如下：</p>
<div class="highlight"><pre><span></span><code><span class="sd">/// The inner task structure.</span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">TaskInner</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">id</span>: <span class="nc">TaskId</span><span class="p">,</span>
<span class="w">    </span><span class="n">name</span>: <span class="nb">String</span><span class="p">,</span>
<span class="w">    </span><span class="n">is_idle</span>: <span class="kt">bool</span><span class="p">,</span>
<span class="w">    </span><span class="n">is_init</span>: <span class="kt">bool</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 任务的入口函数，仅在内核态下有效</span>
<span class="w">    </span><span class="n">entry</span>: <span class="nb">Option</span><span class="o">&lt;*</span><span class="k">mut</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">()</span><span class="o">&gt;</span><span class="p">,</span>
<span class="w">    </span><span class="n">state</span>: <span class="nc">AtomicU8</span><span class="p">,</span>

<span class="w">    </span><span class="n">in_wait_queue</span>: <span class="nc">AtomicBool</span><span class="p">,</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;irq&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="n">in_timer_list</span>: <span class="nc">AtomicBool</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;preempt&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="n">need_resched</span>: <span class="nc">AtomicBool</span><span class="p">,</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;preempt&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">preempt_disable_count</span>: <span class="nc">AtomicUsize</span><span class="p">,</span>

<span class="w">    </span><span class="n">exit_code</span>: <span class="nc">AtomicI32</span><span class="p">,</span>
<span class="w">    </span><span class="n">wait_for_exit</span>: <span class="nc">WaitQueue</span><span class="p">,</span>
<span class="w">    </span><span class="sd">/// 内核栈，对于unikernel不需要</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="n">kstack</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">TaskStack</span><span class="o">&gt;</span><span class="p">,</span>

<span class="w">    </span><span class="n">ctx</span>: <span class="nc">UnsafeCell</span><span class="o">&lt;</span><span class="n">TaskContext</span><span class="o">&gt;</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="c1">// 对应进程ID</span>
<span class="w">    </span><span class="n">process_id</span>: <span class="nc">AtomicU64</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="sd">/// 是否是所属进程下的主线程</span>
<span class="w">    </span><span class="n">is_leader</span>: <span class="nc">AtomicBool</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="c1">// 所属页表ID，在宏内核下默认会开启分页，是只读的所以不用原子量</span>
<span class="w">    </span><span class="n">page_table_token</span>: <span class="kt">usize</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="sd">/// 初始化的trap上下文</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">trap_frame</span>: <span class="nc">UnsafeCell</span><span class="o">&lt;</span><span class="n">TrapFrame</span><span class="o">&gt;</span><span class="p">,</span>
<span class="w">    </span><span class="c1">// 时间统计</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="n">time</span>: <span class="nc">UnsafeCell</span><span class="o">&lt;</span><span class="n">TimeStat</span><span class="o">&gt;</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[allow(unused)]</span>
<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="sd">/// 子线程初始化的时候，存放tid的地址</span>
<span class="w">    </span><span class="n">set_child_tid</span>: <span class="nc">AtomicU64</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="sd">/// 子线程初始化时，将这个地址清空；子线程退出时，触发这里的 futex。</span>
<span class="w">    </span><span class="sd">/// 在创建时包含 CLONE_CHILD_SETTID 时才非0，但可以被 sys_set_tid_address 修改</span>
<span class="w">    </span><span class="n">clear_child_tid</span>: <span class="nc">AtomicU64</span><span class="p">,</span>

<span class="w">    </span><span class="cp">#[cfg(feature = </span><span class="s">&quot;monolithic&quot;</span><span class="cp">)]</span>
<span class="w">    </span><span class="sd">/// 退出时是否向父进程发送SIG_CHILD</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">send_sigchld_when_exit</span>: <span class="kt">bool</span><span class="p">,</span>
<span class="p">}</span>
</code></pre></div>
<p>该种设计的优势如下：</p>
<ul>
<li>保留了 ArceOS 的结构，可以较为方便地与其他同学开发结果进行结合</li>
<li>耦合度低，因此可以使内核较为方便地在不同模式间进行切换</li>
</ul>
<p>在该种设计架构下，接受外来系统调用时，需要将部分对线程进行操作的系统调用转发给进程。进程收
到该系统调用之后，再对当前进程下正在运行的线程进行相应的操作。实例为 yield ， exit 等。</p>
<p>在生成新的任务时，由于是通过Linux的clone调用生成新的任务，因此可以根据clone的参数判断生成的是新的进程还是线程，从而确定线程所属的进程是哪一个，进程与线程之间形成父子关系，而同一进程下的线程形成兄弟关系，从而可以更加方便地进行管理。</p>
<h3 id="_3">地址空间引入<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<h4 id="_4">任务切换<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h4>
<p>引入了进程之后，由于进程是资源容器集合，因此地址空间相关的存储结构也存放在这里，不同进程之间可以共享或者独享地址空间，因此在切换任务时，只需要额外判断当前所属进程的地址空间的token是否发生改变，就可以完成多地址空间的引入。</p>
<h4 id="_5">特权级切换<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h4>
<p>目前内核和用户态使用的是同一个地址空间，可以避免trap时更改页表，减少时空损耗。</p>
<h3 id="_6">特权级切换<a class="headerlink" href="#_6" title="Permanent link">&para;</a></h3>
<p>在Starry中，各种测例运行在用户态下，从内核态进入到用户态的方式有两个：用户程序初始化进入和trap返回。</p>
<h4 id="_7">初始化进入<a class="headerlink" href="#_7" title="Permanent link">&para;</a></h4>
<p>对于用户程序初始化进入部分，即是在原有ArceOS基础上添加了额外的判断：</p>
<p>判断的原则如下：若要执行的任务的入口函数在内核态，则直接调用即可。否则需要通过手写汇编代码保存寄存器，以类似trap返回的机制调用sret进入用户态执行对应的函数。</p>
<div class="highlight"><pre><span></span><code><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="k">fn</span> <span class="nf">task_entry</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">// release the lock that was implicitly held across the reschedule</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">crate</span>::<span class="n">RUN_QUEUE</span><span class="p">.</span><span class="n">force_unlock</span><span class="p">()</span><span class="w"> </span><span class="p">};</span>
<span class="w">    </span><span class="n">axhal</span>::<span class="n">arch</span>::<span class="n">enable_irqs</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">task</span>: <span class="nc">CurrentTask</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">crate</span>::<span class="n">current</span><span class="p">();</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">entry</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">get_process_id</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">KERNEL_PROCESS_ID</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="c1">// 对于unikernel，这里是应用程序的入口，由于都在内核态所以可以直接调用函数</span>
<span class="w">            </span><span class="c1">// 对于宏内核，这是初始调度进程，也在内核态，直接执行即可</span>
<span class="w">            </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">from_raw</span><span class="p">(</span><span class="n">entry</span><span class="p">)()</span><span class="w"> </span><span class="p">};</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="c1">// 需要通过切换特权级进入到对应的应用程序</span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">kernel_sp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">get_kernel_stack_top</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>

<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">frame_address</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">get_first_trap_frame</span><span class="p">();</span>
<span class="w">            </span><span class="c1">// 切换页表已经在switch实现了</span>
<span class="w">            </span><span class="n">first_into_user</span><span class="p">(</span><span class="n">kernel_sp</span><span class="p">,</span><span class="w"> </span><span class="n">frame_address</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="c1">// only for kernel task</span>
<span class="w">    </span><span class="k">crate</span>::<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="sd">/// 初始化主进程的trap上下文</span>
<span class="cp">#[no_mangle]</span>
<span class="k">fn</span> <span class="nf">first_into_user</span><span class="p">(</span><span class="n">kernel_sp</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">frame_base</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">!</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">trap_frame_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">core</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">TrapFrame</span><span class="o">&gt;</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">kernel_base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">kernel_sp</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">trap_frame_size</span><span class="p">;</span>
<span class="w">    </span><span class="c1">// 在保证将寄存器都存储好之后，再开启中断</span>
<span class="w">    </span><span class="c1">// 否则此时会因为写入csr寄存器过程中出现中断，导致出现异常</span>
<span class="w">    </span><span class="n">axhal</span>::<span class="n">arch</span>::<span class="n">disable_irqs</span><span class="p">();</span>
<span class="w">    </span><span class="c1">// 在内核态中，tp寄存器存储的是当前任务的CPU ID</span>
<span class="w">    </span><span class="c1">// 而当从内核态进入到用户态时，会将tp寄存器的值先存储在内核栈上，即把该任务对应的CPU ID存储在内核栈上</span>
<span class="w">    </span><span class="c1">// 然后将tp寄存器的值改为对应线程的tls指针的值</span>
<span class="w">    </span><span class="c1">// 因此在用户态中，tp寄存器存储的值是线程的tls指针的值</span>
<span class="w">    </span><span class="c1">// 而当从用户态进入到内核态时，会先将内核栈上的值读取到某一个中间寄存器t0中，然后将tp的值存入内核栈</span>
<span class="w">    </span><span class="c1">// 然后再将t0的值赋给tp，因此此时tp的值是当前任务的CPU ID</span>
<span class="w">    </span><span class="c1">// 对应实现在axhal/src/arch/riscv/trap.S中</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">asm</span>::<span class="n">sfence_vma_all</span><span class="p">();</span>
<span class="w">        </span><span class="n">core</span>::<span class="n">arch</span>::<span class="fm">asm!</span><span class="p">(</span>
<span class="w">            </span><span class="s">r&quot;</span>
<span class="s">            mv      sp, {frame_base}</span>
<span class="s">            .short  0x2432                      // fld fs0,264(sp)</span>
<span class="s">            .short  0x24d2                      // fld fs1,272(sp)</span>
<span class="s">            LDR     gp, sp, 2                   // load user gp and tp</span>
<span class="s">            LDR     t0, sp, 3</span>
<span class="s">            mv      t1, {kernel_base}</span>
<span class="s">            STR     tp, t1, 3                   // save supervisor tp，注意是存储到内核栈上而不是sp中，此时存储的应该是当前运行的CPU的ID</span>
<span class="s">            mv      tp, t0                      // tp：本来存储的是CPU ID，在这个时候变成了对应线程的TLS 指针</span>
<span class="s">            csrw    sscratch, {kernel_sp}       // put supervisor sp to scratch</span>
<span class="s">            LDR     t0, sp, 31</span>
<span class="s">            LDR     t1, sp, 32</span>
<span class="s">            csrw    sepc, t0</span>
<span class="s">            csrw    sstatus, t1</span>
<span class="s">            POP_GENERAL_REGS</span>
<span class="s">            LDR     sp, sp, 1</span>
<span class="s">            sret</span>
<span class="s">        &quot;</span><span class="p">,</span>
<span class="w">            </span><span class="n">frame_base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">in</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">frame_base</span><span class="p">,</span>
<span class="w">            </span><span class="n">kernel_sp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">in</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">kernel_sp</span><span class="p">,</span>
<span class="w">            </span><span class="n">kernel_base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">in</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">kernel_base</span><span class="p">,</span>
<span class="w">        </span><span class="p">);</span>
<span class="w">    </span><span class="p">};</span>
<span class="w">    </span><span class="n">core</span>::<span class="fm">panic!</span><span class="p">(</span><span class="s">&quot;already in user mode!&quot;</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="trap">trap切换<a class="headerlink" href="#trap" title="Permanent link">&para;</a></h4>
<p>trap切换对应的汇编代码在<code>axhal/src/arch/riscv</code>，值得关注的是其嵌套trap的处理。在第一次进入trap时，是从用户态进入到内核态，此时会将内核栈的地址赋给sp，将用户栈的地址存在内核栈上，并将sscratch清零。若发生内核嵌套trap，则此时sscratch的值为0，与sp交换之后，sp为0，即发生了内核嵌套trap。</p>
<p>因此可以通过交换之后sp是否为0来判断是否发生了内核嵌套trap。</p>





                
              </article>
            </div>
          
          
        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "..", "features": [], "search": "../assets/javascripts/workers/search.74e28a9f.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
    
    
      <script src="../assets/javascripts/bundle.220ee61c.min.js"></script>
      
    
  </body>
</html>